package com.shd.edanpei.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shd.edanpei.R;
import com.shd.edanpei.ui.adapter.ProductAdapter;
import com.shd.edanpei.base.BaseFragment;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.base.rxbus.Subscribe;
import com.shd.edanpei.bean.StatusBean;
import com.shd.edanpei.presenter.contract.ProductContract;
import com.shd.edanpei.presenter.ProductPresenter;
import com.shd.edanpei.ui.activity.ProductDetailsActivity;
import com.shd.edanpei.view.RefreshLayout;

import butterknife.BindView;

import static com.shd.edanpei.common.Constant.TYPE;


/**
 * Created by mc on 2018/4/2.
 * 产品列表
 */

public class ProductFragment extends BaseFragment<ProductPresenter> implements ProductContract.ProductIView {

    @BindView(R.id.refresh_layout)
    RefreshLayout refreshLayout;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    //产品类型 上架 or 下架
    private int mType;//1：上架   0：下架
    private ProductAdapter mAdapter;

    public static ProductFragment newInstance(int i) {
        ProductFragment fragment = new ProductFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(TYPE, i);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected Object setLayout() {
        return R.layout.refresh_list;
    }

    @Override
    protected ProductPresenter bindPresenter() {
        return new ProductPresenter(this);
    }

    @Override
    protected void initView(@Nullable View view, @Nullable Bundle savedInstanceState) {
        RxBus.get().register(this);
        Bundle arguments = getArguments();
        mType = arguments.getInt(TYPE);
        mAdapter = new ProductAdapter(getContext(), mType);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(mAdapter);
        refreshLayout.setRefreshing(true);
        mPresenter.refresh(mType);
    }

    @Override
    protected void initListener() {
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.refresh(mType);
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.loadMore(mType);
            }
        }, recyclerView);
//        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Bundle bundle = new Bundle();
//                bundle.putString(Constant.ID, mAdapter.getData().get(position).getId() + "");
//                bundle.putInt(Constant.INDEX, position);
//                startActivity(ProductDetailsActivity.class, bundle);
//            }
//        });
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.content:
                        Bundle bundle = new Bundle();
                        bundle.putString(Constant.ID, mAdapter.getItem(position).getId() + "");
                        bundle.putInt(Constant.INDEX, position);
                        startActivity(ProductDetailsActivity.class, bundle);
                        break;
                    case R.id.delete:
                        mPresenter.delete(mAdapter.getItem(position).getId(), position);
                        break;
                }
            }
        });

    }

    @Override
    public RefreshLayout getRefreshLayout() {
        return refreshLayout;
    }

    @Override
    public ProductAdapter getAdapter() {
        return mAdapter;
    }


    @Subscribe(code = RxBusCode.NOTIFY_PRODUCT_LIST)
    public void refreshList() {
        refreshLayout.setRefreshing(true);
        mPresenter.refresh(mType);
    }

    @Subscribe(code = RxBusCode.NOTIFY_PRODUCT_LIST)
    public void refreshList(StatusBean bean) {
        if (mType == 1 && bean.isUp() == false) {
            //上架页面，下架成功，将产品从上架列表移除
            mAdapter.remove(bean.getIndex());
            mPresenter.checkList(mType);
        }
        if (mType == 1 && bean.isUp() == true) {
            //上架页面，上架成功，刷新上架产品
            mPresenter.refresh(mType);
        }
        if (mType == 0 && bean.isUp() == false) {
            //下架页面，下架成功，刷新下架列表
            mPresenter.refresh(mType);
        }
        if (mType == 0 && bean.isUp() == true) {
            //下架页面，上架成功，将产品从下架列表移除
            mAdapter.remove(bean.getIndex());
            mPresenter.checkList(mType);
        }

    }

//    @Subscribe(code = RxBusCode.NOTIFY_PRODUCT_LIST)
//    public void notifyAdapter() {
//        if (mType.equals(YesOrNo.YES.getState())
//                && bean.getType().equals(YesOrNo.NO.getState())) {
//            //下架成功，将产品从上架列表移除
//            mAdapter.remove(bean.getIndex());
//            mPresenter.checkList(mType);
//        }
//        if (mType.equals(YesOrNo.NO.getState())
//                && bean.getType().equals(YesOrNo.NO.getState())) {
//            //下架成功，刷新下架列表
//            mPresenter.refresh(mType);
//        }
//        if (mType.equals(YesOrNo.NO.getState())
//                && bean.getType().equals(YesOrNo.YES.getState())) {
//            //上架成功，将产品从下架列表移除
//            mAdapter.remove(bean.getIndex());
//            mPresenter.checkList(mType);
//        }
//        if (mType.equals(YesOrNo.YES.getState())
//                && bean.getType().equals(YesOrNo.YES.getState())) {
//            //上架成功，刷新上架产品
//            mPresenter.refresh(mType);
//        }
//    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus.get().unRegister(this);
    }
}